import { ref } from "vue";

export type BizNatureOption = {
	id: number;
	name: string;
};

export function useBizNature() {
	const pickerRef = ref<any>(null);
	const columns = ref<BizNatureOption[]>([]);
	const show = ref(false);

	const open = (value: number) => {
		let pickerIndex = 0;

		if (value) {
			pickerIndex = columns.value.findIndex((item) => item.id === value);
		}

		pickerRef.value.setIndexs([Math.max(pickerIndex, 0)]);

		show.value = true;
	};

	const close = () => {
		show.value = false;
	};

	const getSelectedName = (id: number) => {
		console.log(id, columns.value);
		const cur = columns.value.find((item) => item.id == id);
		console.log(cur);
		return cur?.name;
	};

	const getData = async () => {
		await uni.$u.get<BizNatureOption[]>("/company/selnature").then((res) => {
			if (res.code === 1) {
				columns.value = res.data;
			}
		});
	};

	getData();

	return {
		pickerRef,
		columns,
		show,
		open,
		close,
		getSelectedName,
	};
}
export function useScaleRange() {
	const pickerRef = ref<any>(null);
	const columns = ref<BizNatureOption[]>([
		{ id: "<10人", name: "<10人" },
		{ id: "10-100人", name: "10-100人" },
		{ id: "100-300人", name: "100-300人" },
		{ id: ">300人", name: ">300人" },
	]);
	const show = ref(false);

	const open = (value: number) => {
		let pickerIndex = 0;

		if (value) {
			pickerIndex = columns.value.findIndex((item) => item.id == value);
		}

		pickerRef.value.setIndexs([Math.max(pickerIndex, 0)]);

		show.value = true;
	};

	const close = () => {
		show.value = false;
	};

	const getSelectedName = (id: number | string) => {
		const cur = columns.value.find((item) => item.id == id);

		return cur?.name || id;
	};

	// const getData = () => {
	// 	uni.$u.get<BizNatureOption[]>("/company/selnature").then((res) => {
	// 		if (res.code === 1) {
	// 			columns.value = res.data;
	// 		}
	// 	});
	// };

	// getData();

	return {
		pickerRef,
		columns,
		show,
		open,
		close,
		getSelectedName,
	};
}
